package com.example.demo1.controller;

import com.example.demo1.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/param")
@RestController
public class ParamController {
    @RequestMapping("/m1")
    public String m1(String name){
        return "接收到的参数name:"+name;
    }
    @RequestMapping("/m2")
    public String m2(String name, Integer age){
        return "接收到的参数name:"+name + ",age:"+age;
    }

    @RequestMapping("/m3")
    public String m3(String name, int age){
        return "接收到的参数name:"+name + ",age:"+age;
    }

    @RequestMapping("/m4")
    public String m4(Person person){
        return "接收到的参数person:"+person.toString();
    }

    @RequestMapping("/m5")
    public String m5(@RequestParam(value = "name", required = false) String username){
        return "接收到的参数name:"+username;
    }

    //传递数组
    @RequestMapping("/m6")
    public String m6(String[] arrayParam){
        return "接收到的参数arrayParam:" + Arrays.toString(arrayParam)+"长度:" + arrayParam.length;
    }

    //传递集合
    //默认是字符串数组,加@RequestParam注解 即可接受为list
    @RequestMapping("/m7")
    public String m7(@RequestParam(required = false) List<String> listParam){
        return "接收到的参数listParam:" + listParam + "长度:" + listParam.size();
    }

    //接受json字符串
    //加@RequestBody注解
    @RequestMapping("/m8")
    public String m8(@RequestBody Person person){
        return "接收的数据person:" + person.toString();
    }

    //获取url中的参数
    //参数前加@PathVariable注解
    @RequestMapping("/m9/{userId}/{name}")
    public String m9(@PathVariable Integer userId,@PathVariable("name") String username){
        return "url中的参数userId:" + userId + "username:" + username;
    }

    //上传图片
    @RequestMapping("/m10")
    public String m10(@RequestPart("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\"+file.getOriginalFilename()));
        return "success";
    }

    //cookie 和 session
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie : cookies) {
//            System.out.println(cookie.getName() + ":" + cookie.getValue());
//        }
        Arrays.stream(cookies).forEach(cookie -> {
            System.out.println(cookie.getName() + ":" + cookie.getValue());
        });
        return "success";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String username, @CookieValue Integer age){
        System.out.println(username);
        System.out.println(age);
        return "success";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        //true 如果不存在 则创建session
        //false 不创建session
        //默认为true
        session.setAttribute("username","zhangsan");
        return "success";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            String username = (String) session.getAttribute("username");
            return "用户:"+username;
        }
        return "session为空";
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(required = false) String username){
        return "username:" + username;
    }

    @RequestMapping("/getSession3")
    //等同于HttpSession session = request.getSession(true);
    //会创建一个session
    public String getSession3(HttpSession session){
        session.setAttribute("password",123);
        String username = (String) session.getAttribute("username");
        Integer password = (Integer) session.getAttribute("password");
        return "用户:"+username + "password:" + password ;
    }

    //获取header
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("user-Agent");
        return "user-Agent:" + userAgent;
    }

    @RequestMapping("getHeader2")
    public String getHeader2(@RequestHeader("user-Agent") String userAgent){
        return "userAgent" + userAgent;
    }

}
